jQuery(document).ready(function() {
//	FB.Flash.hasMinVersion = function () { return false; };
	(function() {
		var e = document.createElement('script');
		e.type = 'text/javascript';
		e.src = document.location.protocol +
			'//connect.facebook.net/en_US/all.js';
		e.async = true;
		document.getElementById('fb-root').appendChild(e);
	}());
	window.fbAsyncInit = function() {
		FB.init({appId: fbAPIid, status: true, cookie: true, xfbml: true});
		
		 // Browser-specific hacks to make FB Connect more reliable
	/*	  $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
		  if ($.browser.chrome || $.browser.msie) {
			FB.XD._origin = window.location.protocol + "//" + document.domain + "/" + FB.guid();
			FB.XD.Flash.init();
			FB.XD._transport = "flash";
		  } else if ($.browser.opera) {
			FB.XD._transport = "fragment";
			FB.XD.Fragment._channelUrl = window.location.protocol + "//" + window.location.host + "/";
		  }
		*/
		if($.browser.msie ) // it uses jQuery library here!  ,channelUrl :BASE_URI+'ranking/fb_channel'
		{
		   //FB.XD._transport="postmessage";
		   //FB.XD.PostMessage.init();
			FB.XD._origin = window.location.protocol + "//" + document.domain + "/" + FB.guid();
			FB.XD.Flash.init();
			FB.XD._transport = "flash";
		}
		
		FB.Event.subscribe('auth.login', function(response) {
			// do something with response
			login();
		});
		FB.Event.subscribe('auth.logout', function(response) {
			// do something with response
			logout();
		});

		FB.getLoginStatus(function(response) {
			if (response.session) {
				// logged in and connected user, someone you know
				login();
			}
		});
	};
	
});



function sendInformation(params){
	$.post(BASE_URI+'ranking/authorization',{data:params},function(res){
		if(res== 'ok'){
			refresh_greeting();
			
		}else{
			alert(res);
			FB.logout();
			return false;
		}			
	});
}

function login(){
	FB.api('/me', function(response) {
	 	if(json_AUTHOR== null){
	 		var params= {stt:'login',fb_id:response.id,user_name:response.name,gender:response.gender};
	 		sendInformation(params);
	 	}
	});
}

function logout(){
	$data = {stt:'logout'};
 	$.post(BASE_URI+'ranking/authorization',{data:$data},function(res){
		refresh_greeting();
	});
}

function refresh_greeting(){
	$.getScript( BASE_URI + 'ranking/js_author' , function(data, textStatus){
		display_greeting();
	});
}

